package org.example.lethear.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 举报实体类
 */
@Entity
@Table(name = "reports")
@Data
@EqualsAndHashCode(callSuper = false)
@EntityListeners(AuditingEntityListener.class)
public class Report {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "reporter_id", nullable = false)
    private Long reporterId; // 举报者ID
    
    @Column(name = "target_type", nullable = false)
    private Integer targetType; // 举报目标类型：1-帖子，2-评论，3-用户，4-悄悄话
    
    @Column(name = "target_id", nullable = false)
    private Long targetId; // 举报目标ID
    
    @Column(name = "reason_type", nullable = false)
    private Integer reasonType; // 举报原因类型：1-垃圾信息，2-违法违规，3-涉黄涉暴，4-恶意骚扰，5-其他
    
    @Column(name = "reason_detail", length = 500)
    private String reasonDetail; // 详细原因描述
    
    @Column(name = "evidence_urls", columnDefinition = "TEXT")
    private String evidenceUrls; // 证据URL，JSON格式存储
    
    @Column(columnDefinition = "TINYINT DEFAULT 0")
    private Integer status; // 处理状态：0-待处理，1-已处理，2-已驳回，3-已删除目标内容
    
    @Column(name = "handler_id")
    private Long handlerId; // 处理者ID（管理员）
    
    @Column(name = "handle_time")
    private LocalDateTime handleTime; // 处理时间
    
    @Column(name = "handle_result", length = 500)
    private String handleResult; // 处理结果说明
    
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;
    
    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reporter_id", insertable = false, updatable = false)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private User reporter;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "handler_id", insertable = false, updatable = false)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private User handler;
    
    // 构造方法
    public Report() {}
    
    public Report(Long reporterId, Integer targetType, Long targetId, Integer reasonType, String reasonDetail) {
        this.reporterId = reporterId;
        this.targetType = targetType;
        this.targetId = targetId;
        this.reasonType = reasonType;
        this.reasonDetail = reasonDetail;
        this.status = 0; // 默认待处理
    }
    
    // 常量定义
    public static final int TARGET_TYPE_POST = 1;
    public static final int TARGET_TYPE_COMMENT = 2;
    public static final int TARGET_TYPE_USER = 3;
    public static final int TARGET_TYPE_WHISPER = 4;
    
    public static final int REASON_TYPE_SPAM = 1;
    public static final int REASON_TYPE_ILLEGAL = 2;
    public static final int REASON_TYPE_INAPPROPRIATE = 3;
    public static final int REASON_TYPE_HARASSMENT = 4;
    public static final int REASON_TYPE_OTHER = 5;
    
    public static final int STATUS_PENDING = 0;
    public static final int STATUS_RESOLVED = 1;
    public static final int STATUS_REJECTED = 2;
    public static final int STATUS_CONTENT_DELETED = 3;
    
    // 便利方法
    public boolean isPending() {
        return status != null && status == STATUS_PENDING;
    }
    
    public boolean isResolved() {
        return status != null && status == STATUS_RESOLVED;
    }
    
    public boolean isRejected() {
        return status != null && status == STATUS_REJECTED;
    }
    
    public boolean isContentDeleted() {
        return status != null && status == STATUS_CONTENT_DELETED;
    }
    
    public String getTargetTypeName() {
        return switch (targetType) {
            case TARGET_TYPE_POST -> "帖子";
            case TARGET_TYPE_COMMENT -> "评论";
            case TARGET_TYPE_USER -> "用户";
            case TARGET_TYPE_WHISPER -> "悄悄话";
            default -> "未知";
        };
    }
    
    public String getReasonTypeName() {
        return switch (reasonType) {
            case REASON_TYPE_SPAM -> "垃圾信息";
            case REASON_TYPE_ILLEGAL -> "违法违规";
            case REASON_TYPE_INAPPROPRIATE -> "涉黄涉暴";
            case REASON_TYPE_HARASSMENT -> "恶意骚扰";
            case REASON_TYPE_OTHER -> "其他";
            default -> "未知";
        };
    }
    
    public String getStatusName() {
        return switch (status) {
            case STATUS_PENDING -> "待处理";
            case STATUS_RESOLVED -> "已处理";
            case STATUS_REJECTED -> "已驳回";
            case STATUS_CONTENT_DELETED -> "已删除内容";
            default -> "未知";
        };
    }
    
    // 静态工厂方法
    public static Report createPostReport(Long reporterId, Long postId, Integer reasonType, String reasonDetail) {
        return new Report(reporterId, TARGET_TYPE_POST, postId, reasonType, reasonDetail);
    }
    
    public static Report createCommentReport(Long reporterId, Long commentId, Integer reasonType, String reasonDetail) {
        return new Report(reporterId, TARGET_TYPE_COMMENT, commentId, reasonType, reasonDetail);
    }
    
    public static Report createUserReport(Long reporterId, Long userId, Integer reasonType, String reasonDetail) {
        return new Report(reporterId, TARGET_TYPE_USER, userId, reasonType, reasonDetail);
    }
    
    public static Report createWhisperReport(Long reporterId, Long whisperId, Integer reasonType, String reasonDetail) {
        return new Report(reporterId, TARGET_TYPE_WHISPER, whisperId, reasonType, reasonDetail);
    }
} 